﻿/*
 * File: GamePlayBaseScene_Helper.cs
 * Author: Gourav Das
 * Purpose: Declaratyion and helper methods for 3D gameplay
 * Created: Jun17 2011
 */

using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Threading;
using System.IO;
using _22yards._2D;
using _22yards.Scenes.Helpers;
using _22yards.Core;
using _22yards;
using _22yards.EnumDeclarations;
using _22yards.ThreeD;
using _22yards.Helpers;
using Komires.MataliPhysics;

namespace _22yards.Scenes
{
    /// <summary>
    /// This is a scene that implements the Base Gameplay scene
    /// </summary>
    partial class GamePlayBaseScene : Scene
    {
        #region General Base Gameplay Scene

        private RenderTarget2D RenderTarget3DScene;    
        private Texture2D Original3DTexture;

        private RenderTarget2D ShadowMapTarget;
        private Texture2D ShadowMapTexture;

        Constraint constraint;

        private BaseCamera broadcastCamera;
        private BroadcastBallChaseCamera broadcastBallChaseCam;
        
        //Stadium
        private StadiumModel stadia;
        private Texture2D TexGround;
        private Texture2D TexGroundNormal;
        private Texture2D TexGroundSpec;
        private Texture2D TexGroundPitch;
        private Texture2D TexGroundPitchNormal;
        private Texture2D TexGroundPitchSpec;
        private Texture2D TexRope;
        private Texture2D TexRopeNormal;
        private Texture2D TexRopeSpec;

        CustomModel LeftStumpFarEnd;
        CustomModel RightStumpFarEnd;
        CustomModel CenterStumpFarEnd;
        CustomModel LeftStumpNearEnd;
        CustomModel RightStumpNearEnd;
        CustomModel CenterStumpNearEnd;
        private Texture2D TexSideStump;
        private Texture2D TexCenterStump;
        private Texture2D TexSideStumpNormal;
        private Texture2D TexCenterStumpNormal;
        private Texture2D TexSideStumpSpec;
        private Texture2D TexCenterStumpSpec;

        CustomModel Pitch;
        private Texture2D TexPitch;
        private Texture2D TexPitchNormal;
        private Texture2D TexPitchSpec;
        private Texture2D TexPitchHeight;

        CustomModel LeftBailFarEnd;
        CustomModel RightBailFarEnd;
        CustomModel LeftBailNearEnd;
        CustomModel RightBailNearEnd;
        private Texture2D TexBail;
        private Texture2D TexBailNormal;
        private Texture2D TexBailSpec;

        private CustomModel KBRedBall;
        private Texture2D TexKBRedBall;
        private Texture2D TexKBRedBallNormal;
        private Texture2D TexKBRedBallSpec;
        private CustomModel Fielder1;

        private PhysicsEngine PhysxEngine;
        private PhysicsScene PhysxScene;
        private Vector3 Gravity;

        private PhysicsObject BallObject;
        private PhysicsObject FieldObject;
        private PhysicsObject RopeObject;
        private PhysicsObject PitchObject;
        private PhysicsObject LeftStumpFarEndObject;
        private PhysicsObject CenterStumpFarEndObject;
        private PhysicsObject RightStumpFarEndObject;
        private PhysicsObject LeftStumpNearEndObject;
        private PhysicsObject CenterStumpNearEndObject;
        private PhysicsObject RightStumpNearEndObject;
        private PhysicsObject LeftBailFarEndObject;
        private PhysicsObject RightBailFarEndObject;
        private PhysicsObject LeftBailNearEndObject;
        private PhysicsObject RightBailNearEndObject;

        #endregion

        void FireBall()
        {
            broadcastBallChaseCam.ResetCamera();
            birdEyeViewChaseCamera.ResetCamera();

            LeftStumpFarEndObject.InitLocalTransform.SetPosition(new Vector3(1.98f, PHYSICSSTUMPHALFHEIGHT, 224f));
            LeftStumpFarEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(LeftStumpFarEndObject);
            RightStumpFarEndObject.InitLocalTransform.SetPosition(new Vector3(-1.98f, PHYSICSSTUMPHALFHEIGHT, 224f));
            RightStumpFarEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(RightStumpFarEndObject);
            CenterStumpFarEndObject.InitLocalTransform.SetPosition(new Vector3(0f, PHYSICSSTUMPHALFHEIGHT, 224f));
            CenterStumpFarEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(CenterStumpFarEndObject);
            LeftStumpNearEndObject.InitLocalTransform.SetPosition(new Vector3(1.98f, PHYSICSSTUMPHALFHEIGHT, -126f));
            LeftStumpNearEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(LeftStumpNearEndObject);
            RightStumpNearEndObject.InitLocalTransform.SetPosition(new Vector3(-1.98f, PHYSICSSTUMPHALFHEIGHT, -126f));
            RightStumpNearEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(RightStumpNearEndObject);
            CenterStumpNearEndObject.InitLocalTransform.SetPosition(new Vector3(0f, PHYSICSSTUMPHALFHEIGHT, -126f));
            CenterStumpNearEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(CenterStumpNearEndObject);
            LeftBailFarEndObject.InitLocalTransform.SetPosition(new Vector3(1f, PHYSICSSTUMPHALFHEIGHT * 2f + 0.26f, 224f));
            LeftBailFarEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(LeftBailFarEndObject);
            RightBailFarEndObject.InitLocalTransform.SetPosition(new Vector3(-1f, PHYSICSSTUMPHALFHEIGHT * 2f + 0.26f, 224f));
            RightBailFarEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(RightBailFarEndObject);
            LeftBailNearEndObject.InitLocalTransform.SetPosition(new Vector3(1f, PHYSICSSTUMPHALFHEIGHT * 2f + 0.26f, -126f));
            LeftBailNearEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(LeftBailNearEndObject);
            RightBailNearEndObject.InitLocalTransform.SetPosition(new Vector3(-1f, PHYSICSSTUMPHALFHEIGHT * 2f + 0.26f, -126f));
            RightBailNearEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(RightBailNearEndObject);
            BallObject.InitLocalTransform.SetPosition(new Vector3(2f, 35f, -130f));
            BallObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(BallObject);
            constraint.Update();
        }
    }
}